package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunxinyong.cec.entity.Cec711SixFinanceDebt;
import net.yunxinyong.cec.entity.Cec711SixFinanceDebtExample;
import net.yunxinyong.cec.mapper.Cec711SixFinanceDebtMapper;
import net.yunxinyong.cec.service.Cec711SixDebtService;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Cec711SixDebtServiceImpl implements Cec711SixDebtService {
    @Autowired
    private Cec711SixFinanceDebtMapper cec711SixFinanceDebtMapper;
    @Override
    public Cec711SixFinanceDebt insertSixDebt(Cec711SixFinanceDebt cec711SixFinanceDebt) {
        //通过base_id判断企业是否存在
        Cec711SixFinanceDebtExample cec711SixFinanceDebtExample = new Cec711SixFinanceDebtExample();
        cec711SixFinanceDebtExample.createCriteria().andBaseIdEqualTo(cec711SixFinanceDebt.getBaseId());
        List<Cec711SixFinanceDebt> cec711SixFinanceDebtList = cec711SixFinanceDebtMapper.selectByExample(cec711SixFinanceDebtExample);
        if(!cec711SixFinanceDebtList.isEmpty()){
            return null;
        }
        int i = cec711SixFinanceDebtMapper.insertSelective(cec711SixFinanceDebt);
        return cec711SixFinanceDebt;
    }

    @Override
    public int deleteSixDebt(Integer id) {
        //根据baseId删除企业信息
        Cec711SixFinanceDebt cec711SixFinanceDebt = new Cec711SixFinanceDebt();
        cec711SixFinanceDebt.setBaseId(id);
        cec711SixFinanceDebt.setStatus(0);
        int i = cec711SixFinanceDebtMapper.updateByPrimaryKeySelective(cec711SixFinanceDebt);
        return i;
    }

    @Override
    public int updateSixDebt(Cec711SixFinanceDebt cec711SixFinanceDebt) {
        cec711SixFinanceDebt.setUpdateTime(new Date());
        int i = cec711SixFinanceDebtMapper.updateByPrimaryKeySelective(cec711SixFinanceDebt);
        return i;
    }

    @Override
    public PageBean ListSixDebt(Cec711SixFinanceDebt cec711SixFinanceDebt, int page, int rows) {
        if(page != 0 && rows !=0){
            PageHelper.startPage(page,rows);
        }
        Cec711SixFinanceDebtExample cec711SixFinanceDebtExample = new Cec711SixFinanceDebtExample();
        Cec711SixFinanceDebtExample.Criteria criteria = cec711SixFinanceDebtExample.createCriteria();

        List<Cec711SixFinanceDebt> cec711SixFinanceDebtList = cec711SixFinanceDebtMapper.selectByExample(cec711SixFinanceDebtExample);
        PageBean<Cec711SixFinanceDebt> pageBean = new PageBean<>();
        PageInfo<Cec711SixFinanceDebt> pageInfo = new PageInfo<>(cec711SixFinanceDebtList);
        pageBean.setPageDatas(cec711SixFinanceDebtList);
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        return pageBean;
    }


}
